//
//  AppDelegate.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/1.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UIAlertViewDelegate {
    
    var window: UIWindow?
    
    let tool = WXTool()
    
    lazy var wx_tool = Weixin()
    
    let qqTool = QQTool()
    
    lazy var tencentOAuth = TencentOAuth()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // 创建window
        
//       //启动主界面
        setMainBar()
//
//        //启动登陆界面
////        loginSEL()
//        
//        //监听网络
//        CheckNetTool.shareInstance.checkNet()
        
        
        
//        setThird()

        return true
    }
}

// MARK:- 第三方
extension AppDelegate {
    ///第三方登陆
    func setThird() -> Void {
        window = UIWindow(frame: UIScreen.main.bounds)
        let vc = ViewController(nibName: "ViewController", bundle: nil)
        
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
        setQQ()
        setWX()
    }
}


// MARK:- 登录
extension AppDelegate {
    func loginSEL() -> Void {
        // 创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        ///启动界面加导航栏
        let nav : UINavigationController = UINavigationController(rootViewController: UserVC())
        
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
    }
}

// MARK:- 设置主控制器
extension AppDelegate {
    func setMainBar() -> Void {
        // 创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        
        window?.makeKeyAndVisible()
        
        print("\((#file as NSString).lastPathComponent):(\(#line))")

        let customtabbar = MainViewController()
        window?.rootViewController = customtabbar
        
        setQQ()
        setWX()

        // 设置全局颜色
        UITabBar.appearance().tintColor = UIColor.orange
        UINavigationBar.appearance().tintColor = UIColor.orange
    }
}

// MARK:- 第三方介入
extension AppDelegate {
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let wxUrl = String(format: "%@", arguments: [app_key])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
            //            return WXApi.handleOpen(url, delegate: tool)
            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
        }
        return true;
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        
        let wxUrl = String(format: "%@", arguments: [app_key])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
            //            return WXApi.handleOpen(url, delegate: tool)
            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
        }
        return true;
    }
    
    // MARK:- 微信分享
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        
        let wxUrl = String(format: "%@", arguments: [app_key])
        let qqUrl = String(format: "tencent%@", arguments: [QQAppID])
        
        if url.absoluteString.hasPrefix(wxUrl) {
            //            return WXApi.handleOpen(url, delegate: tool)
            return WXApi.handleOpen(url, delegate: wx_tool)
        } else if url.absoluteString.hasPrefix(qqUrl){
            QQApiInterface.handleOpen(url, delegate: qqTool)
            return TencentOAuth.handleOpen(url)
        }
        return true;
    }
}

// MARK:- 微信
extension AppDelegate {
    
    func setWX() -> Void {
        WXApi.registerApp(app_key)
        wx_tool.requestForUserInfoBlock = {() -> Void in
            Weixin.shareDataSource().wechatLoginByRequestForUserInfo()
        }
    } 
}

// MARK:- QQ
extension AppDelegate {
    func setQQ() -> Void {
        tencentOAuth = TencentOAuth(appId: QQAppID, andDelegate: qqTool)
    }
}




 
